package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.ApplyWayBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.ada.UnionPayAda;
import com.bocai.kelalacar.ui.car.set.contract.WithdrawTypeContract;
import com.bocai.kelalacar.ui.car.set.module.WithdrawTypeModule;
import com.bocai.kelalacar.ui.car.set.presenter.WithdrawTypePresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/22.
 */

public class UnionPayManageActivity extends BaseActivity implements WithdrawTypeContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    @BindView(R.id.btn_add_union)
    Button btnAddUnion;
    UnionPayAda ada;
    @Inject
    WithdrawTypePresenter presenter;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Override
    public int getLayoutId() {
        return R.layout.act_union_pay_manage;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new WithdrawTypeModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("提现方式");
        initBack(imgBack,llBack);
//        arrayList = getIntent().getParcelableArrayListExtra("data");
        ada = new UnionPayAda();
        ada.setPresenter(presenter);
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setText("管理");
        xrecycle.setLayoutManager(new LinearLayoutManager(this));
        xrecycle.setAdapter(ada);
        xrecycle.setLoadingMoreEnabled(false);
        xrecycle.setPullRefreshEnabled(false);
//        if (arrayList != null) {
//            ada.addItems(arrayList);
//        }
        presenter.applyWay();

}


    @OnClick({R.id.right_btn,R.id.btn_add_union})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.right_btn:
                if (rightBtn.getText().toString().equals("管理")) {
                    ada.setShow(true);
                    rightBtn.setText("取消");
                }else {
                    ada.setShow(false);
                    rightBtn.setText("管理");
                }
                break;
            case R.id.btn_add_union:
                startActivityForResult(new Intent(UnionPayManageActivity.this,AddUnionPayActivity.class),11);
                break;
        }
    }

    @Override
    public void applyWaySuccess(ApplyWayBean bean) {
        if (bean!=null&&bean.getBankCard()!=null){
            ada.clear();
            ada.addItems(bean.getBankCard());
        }
    }

    @Override
    public void addApplyWaySuccess() {

    }

    @Override
    public void deleteApplyWaySuccess(int index) {
        ada.remove(index);
    }

    @Override
    public void walletApplySuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }else if (msg.equals("网络异常")){
            xrecycle.refreshComplete();
            xrecycle.loadMoreComplete();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 11){
            presenter.applyWay();
        }
    }
}
